package com.hcrazy.manchester.app.jdbc;

import java.util.ArrayList;
import java.util.List;

import com.hcrazy.manchester.app.pojo.PageModel;
import com.hcrazy.manchester.app.pojo.RadioChannel;
import com.hcrazy.manchester.app.utils.Constant;

public class RadioChannelDAO extends BaseDAO<RadioChannel>{

	public long addRadioChannel(RadioChannel channel) throws Exception{
		StringBuilder sb = new StringBuilder();
		sb.append("insert into him_radio_channel(");
		sb.append("channelNum,channelName,channelDesc,channelLogo,detailDesc,cType,cScene,cStyle,createUser,createDate,praiseBaseCount,praiseCount,commentBaseCount,commentCount,views");
		sb.append(") values (");
		sb.append(":channelNum,:channelName,:channelDesc,:channelLogo,:detailDesc,:cType,:cScene,:cStyle,:createUser,:createDate,:praiseBaseCount,:praiseCount,:commentBaseCount,:commentCount,:views");
		sb.append(")");
		
		update(sb.toString(), channel);
		
		return getIncreamentId();
	}
	
	public void updateRadioChannel(RadioChannel channel) throws Exception{
		StringBuilder sb = new StringBuilder();
		sb.append("update him_radio_channel set ");
		sb.append(" channelNum=:channelNum,channelName=:channelName,channelDesc=:channelDesc,channelLogo=:channelLogo,detailDesc=:detailDesc,");
		sb.append(" cType=:cType,cScene=:cScene,cStyle=:cStyle,createUser=:createUser,");
		sb.append(" createDate=:createDate,views=:views,praiseBaseCount=:praiseBaseCount,praiseCount=:praiseCount,commentBaseCount=:commentBaseCount");
		sb.append(" where channelId = :channelId");
		
		update(sb.toString(), channel);
	}
	
	public List<RadioChannel> listAllInfos() throws Exception{
		String sql = "select * from him_radio_channel";
		return getList(sql, RadioChannel.class);
	}
	
	public List<RadioChannel> getAllInfosBySortId(int cType,int cScene,int cStyle,String search) throws Exception{
		StringBuffer sb = new StringBuffer();
		sb.append("select * from him_radio_channel where 1=1");
		
		if(cType > 0){
			sb.append(" and (cType like '%,"+cType+"'");
			sb.append(" or cType like '"+cType+",%'");
			sb.append(" or cType like '%,"+cType+",%'");
			sb.append(" or cType =" + cType+")");
		}
		
		if(cScene > 0){
			sb.append(" and (cScene like '%,"+cScene+"'");
			sb.append(" or cScene like '"+cScene+",%'");
			sb.append(" or cScene like '%,"+cScene+",%'");
			sb.append(" or cScene =" + cScene+")");
		}
		
		if(cStyle > 0){
			sb.append(" and (cType like '%,"+cStyle+"'");
			sb.append(" or cType like '"+cStyle+",%'");
			sb.append(" or cType like '%,"+cStyle+",%'");
			sb.append(" or cType =" + cStyle+")");
		}
		
		if(null != search){
			sb.append(" and (channelName like '%"+search+"%' or channelNum like '%"+search+"%')");
			
		}
		
		return getList(sb.toString(), RadioChannel.class);
	}
	
	/*public List<RadioChannel> getAllInfosHasDetailBySortId(int sortId) throws Exception{
		String sql = "select a.*,b.sortName from him_radio_channel a,him_news_sort b where a.sortId = b.sortId and a.sortId = ?";
		return getList(sql, RadioChannel.class,sortId);
	}*/
	
	public PageModel getPagedInfos(PageModel model,String search,int cType,int cScene,int cStyle) throws Exception{
		StringBuilder querySQL = new StringBuilder("select * from him_radio_channel");
		StringBuilder countSQL = new StringBuilder("select count(*) from him_radio_channel ");
		StringBuilder whereSQL = new StringBuilder(" where (channelNum like ? or channelName like ?)");
		List<Object> paramList = new ArrayList<Object>();
		if(null == search){
			search = "";
		}
		paramList.add("%"+search+"%");
		paramList.add("%"+search+"%");

		if(cType > 0){
			whereSQL.append(" and (cType like ? or cType like ? or cType like ? or cType = ?)");
			paramList.add("%,"+cType);
			paramList.add(cType+",%");
			paramList.add("%,"+cType+",%");
			paramList.add(cType);
		}
		
		if(cScene > 0){
			whereSQL.append(" and (cScene like ? or cScene like ? or cScene like ? or cScene = ?)");
			paramList.add("%,"+cScene);
			paramList.add(cScene+",%");
			paramList.add("%,"+cScene+",%");
			paramList.add(cScene);
		}
		
		if(cStyle > 0){
			whereSQL.append(" and (cStyle like ? or cStyle like ? or cStyle like ? or cStyle = ?)");
			paramList.add("%,"+cStyle);
			paramList.add(cStyle+",%");
			paramList.add("%,"+cStyle+",%");
			paramList.add(cStyle);
		}
		
		
		whereSQL.append(" order by createDate desc");
		
		System.out.println(querySQL+whereSQL.toString());
		System.out.println(paramList);
		return this.getPageModel(model, querySQL, countSQL, whereSQL, paramList, RadioChannel.class);
	}
	
	
	public RadioChannel getById(int channelId) throws Exception{
		String sql = "select * from him_radio_channel where channelId = ?";
		return this.getJavaBean(sql, RadioChannel.class, channelId);
	}
	
	/*public RadioChannel getById(int channelId,boolean hasDetail) throws Exception{
		if(hasDetail){
			return getById(channelId);
		}
		String sql = "select a.newsTitle,a.newsDesc,a.author,a.userId,a.publishTime," +
				"a.newsLogo,a.newsImages,a.showInIndex,a.sortId,a.views,a.praiseBaseCount," +
				"a.praiseCount,a.commentBaseCount,a.commentCount,a.newsType,b.sortName " +
				"from him_radio_channel where channelId = ?";
		return this.getJavaBean(sql, RadioChannel.class, channelId);
	}*/
	
	public void delRadioChannel(int channelId) throws Exception{
		String sql = "delete from him_radio_channel where channelId = ?";
		this.commonUpdate(sql, channelId);
	}
	
	public long getPraiseCount(long channelId) throws Exception{
		String sql = "select praiseCount from him_radio_channel where channelId = ?";
		return this.getJdbcTemplate().queryForLong(sql,channelId);
	}
	
	public long getPraiseBaseCount(long channelId) throws Exception{
		String sql = "select praiseBaseCount from him_radio_channel where channelId = ?";
		return this.getJdbcTemplate().queryForLong(sql,channelId);
	}
	
	public long getCommentCount(long channelId) throws Exception{
		String sql = "select commentCount from him_radio_channel where channelId = ?";
		return this.getJdbcTemplate().queryForLong(sql,channelId);
	}
	
	public long getCommentBaseCount(long channelId) throws Exception{
		String sql = "select commentBaseCount from him_radio_channel where channelId = ?";
		return this.getJdbcTemplate().queryForLong(sql,channelId);
	}
	
	public void addPraiseCount(long channelId) throws Exception {
		String sql = "update him_radio_channel set praiseCount = praiseCount + 1 where channelId = ?";
		this.commonUpdate(sql, channelId);
	}
	
	public void addCommentCount(long channelId) throws Exception {
		String sql = "update him_radio_channel set commentCount = commentCount + 1 where channelId = ?";
		this.commonUpdate(sql, channelId);
	}
	
	/**
	 * 获取收藏信息
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	public List<RadioChannel> getCollegeChannels(long userId,int moduleType) throws Exception{
		
		StringBuffer sb = new StringBuffer();
		sb.append("select a.type,b.* from him_user_collection a,him_radio_channel b");
		sb.append(" where a.userId = ?");
		sb.append(" and a.type = ? and a.infoId = b.channelId ");
		
		return this.getList(sb.toString(), RadioChannel.class,userId,moduleType);
	}
	
	public int getCount() throws Exception{
		return super.getCount("select count(channelId) from him_radio_channel");
	}
	
}
